<template>
  <van-address-edit
    :area-list="areaList"
    show-delete
    :address-info="addressInfo"
    save-button-text="修改"
    @save="onSave"
    @delete="onDelete"
  />
</template>
<script lang="ts">
import { Vue, Component } from "vue-property-decorator";
import axios from "axios";
import AreaList from "@/api/area";
import { addressForm } from "@/form/addressForm";
import { Toast } from "vant";

@Component
export default class AddressEdit extends Vue {
  private addressInfo = {};
  private areaList = AreaList;

  protected created() {
    let data = JSON.parse(this.$route.query.item);
    console.log(data);
    this.addressInfo = data;
    let index = data.address.indexOf("区");
    if (index < 0) index = data.address.indexOf("县");
    this.addressInfo.addressDetail = data.address.substring(index + 1);
  }

  protected onSave(item: addressForm) {
    axios.put("http://localhost:8081/address/update", item).then((res) => {
      if (res.data.code == 0) {
        let instance = Toast("修改成功");
        setTimeout(() => {
          instance.close();
          this.$router.push("/addressList");
        }, 1000);
      }
    });
  }

  protected onDelete() {
    history.go(-1);
  }
}
</script>
